import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import { WeaLocaleProvider,WeaTools } from 'ecCom';
import * as API from '../apis/batchPrint';
import * as _ from 'lodash';
const getLabel = WeaLocaleProvider.getLabel;
const Base64 = WeaTools.Base64;

export class WFBatchPrintIndexStore {

    @observable loading = false;
    title;
    workflowid = "";
    urlParams = {};
    fixedCondition = [];
    fieldCondition = [];
    nodeOption = [];
    @observable fixedForm = new WeaForm();
    @observable fieldForm = null;
    @observable showDetailField = false;
    
    constructor(){
        this.setUrlParams = this.setUrlParams.bind(this);
        this.loadFixedCondition = this.loadFixedCondition.bind(this);
        this.changeWorkflowid = this.changeWorkflowid.bind(this);
        this.loadFieldCondition = this.loadFieldCondition.bind(this);
        this.controlShowDetailField = this.controlShowDetailField.bind(this);
        this.appendFixedFormFields = this.appendFixedFormFields.bind(this);
        this.resetCondition = this.resetCondition.bind(this);
    }

    @action
    setUrlParams(params = {}){
        this.urlParams = params;
        this.title = getLabel(26382,"批量打印");
        if(this.urlParams.tabTitle){
            this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
        }
    }

    loadFixedCondition(){
        this.resetCondition(true);
        this.loading = true;
        API.getFixedCondition(this.urlParams).then(datas =>{
            this.loading = false;
            this.fixedCondition = datas.condition;
            this.fixedForm.initFormFields(datas.condition);
        });
    }

    changeWorkflowid(workflowid=""){
        if(workflowid === "")
            this.clearFieldCondition();
        else
            this.loadFieldCondition({workflowid});
    }

    setWorkflowid(workflowid=""){
        this.workflowid = workflowid;
    }

    loadFieldCondition(params={}){
        if(_.isEmpty(params) || !params.workflowid)
            return;
        this.loading = true;
        API.getFormFieldCondition({...this.urlParams, ...params}).then(datas =>{
            this.loading = false;
            //this.workflowid = datas.workflowid;
            this.nodeOption = datas.nodeOption;
            this.showDetailField = false;
            this.fieldForm = new WeaForm();
            this.fieldCondition = datas.fieldCondition;
            this.fieldForm.initFormFieldObj(datas.fieldCondition);
        });
    }

    controlShowDetailField(v){
        this.showDetailField = parseInt(v) === 1;
    }

    appendFixedFormFields(value){
        this.fixedForm.updateFields(value, false);
    }

    resetCondition(reset = false){
        if(reset){
            this.fixedForm = new WeaForm();
            this.fixedCondition = [];
        }else{
            this.fixedForm && this.fixedForm.resetConditionValue();
        }
        this.workflowid = "";
        this.clearFieldCondition();
    }

    clearFieldCondition(){
        this.nodeOption = [];
        this.showDetailField = false;
        this.fieldForm = null;
        this.fieldCondition = [];
    }

}